package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjUser;
import com.moji.entity.MjWorkOrder;
import com.moji.entity.MjWorkOrderDetail;
import com.moji.entity.SysMessage;
import com.moji.entity.dto.QueryWorkOrderDto;
import com.moji.entity.vo.MjWorkOrderVo;
import com.moji.mapper.MjWorkOrderMapper;
import com.moji.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电子工单表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-19
 */
@Service
public class MjWorkOrderServiceImpl extends ServiceImpl<MjWorkOrderMapper, MjWorkOrder> implements MjWorkOrderService {

    @Resource
    private MjWorkOrderDetailService mjWorkOrderDetailService;

    @Resource
    private MjUserService mjUserService;

    @Resource
    private SysMessageService sysMessageService;

    @Resource
    private DataDictionaryService dataDictionaryService;

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjWorkOrder> queryByPage(QueryWorkOrderDto dto) {
        LambdaQueryWrapper<MjWorkOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getWorkName()),MjWorkOrder::getWorkName,dto.getWorkName());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getWorkType()),MjWorkOrder::getWorkType,dto.getWorkType());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getStatus()),MjWorkOrder::getStatus,dto.getStatus());
        wrapper.eq(MjWorkOrder::getCreateId,MoJiCommonUtils.getUserCode());
        wrapper.orderByDesc(MjWorkOrder::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjWorkOrder> mjWorkOrderList = list(wrapper);
        //工单类型
        Map<String, String> map = dataDictionaryService.getMapByDictionaryCode("WORKTYPE");
        mjWorkOrderList.forEach(mjWorkOrder -> {
            mjWorkOrder.setWorkType(map.get(mjWorkOrder.getWorkType()));
        });
        PageInfo<MjWorkOrder> info = new PageInfo<>(mjWorkOrderList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjWorkOrder mjWorkOrder) {
        String workCode= idGenerator.idGenerator("DZGD");
        mjWorkOrder.setWorkCode(workCode);
        if(ObjectUtil.isEmpty(mjWorkOrder.getStatus())){
            mjWorkOrder.setStatus(0);
        }
        mjWorkOrder.setUserCode(MoJiCommonUtils.getUserCode());
        mjWorkOrder.setUserName(MoJiCommonUtils.getUserName());
        mjWorkOrder.setMobile(MoJiCommonUtils.getUserPhone());
        mjWorkOrder.setCreateId(MoJiCommonUtils.getUserCode());
        mjWorkOrder.setUpdateId(MoJiCommonUtils.getUserCode());
        mjWorkOrder.setCreateTime(new Date());
        mjWorkOrder.setUpdateTime(new Date());
        return this.save(mjWorkOrder);
    }

    @Override
    public boolean edit(MjWorkOrder mjWorkOrder) {
        MjWorkOrder mjWorkOrder1=this.getById(mjWorkOrder.getId());
        BeanUtils.copyProperties(mjWorkOrder,mjWorkOrder1);
        mjWorkOrder1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjWorkOrder1.setUpdateTime(new Date());
        return this.updateById(mjWorkOrder1);
    }

    @Override
    public MjWorkOrderVo detail(Long id) {
        MjWorkOrder mjWorkOrder=this.getById(id);
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("工单数据不存在,请核查!");
        }
        MjWorkOrderVo mjWorkOrderVo=new MjWorkOrderVo();
        BeanUtils.copyProperties(mjWorkOrder,mjWorkOrderVo);

        List<MjWorkOrderDetail> workOrderDetails=mjWorkOrderDetailService.list(new LambdaQueryWrapper<MjWorkOrderDetail>()
        .eq(MjWorkOrderDetail::getWorkCode,mjWorkOrder.getWorkCode()));
        mjWorkOrderVo.setWorkOrderDetails(workOrderDetails);
        return mjWorkOrderVo;
    }
}
